import request from "./services/request";
import { useState } from 'react'
import { history } from 'umi';

//取得Token    
const token = sessionStorage.getItem('token')

// 判断Token是否过期
if (!token) {
  //清除
  localStorage.clear();
}
// 判断是否有userInfo
const userInfo = localStorage.getItem('userInfo')
if (!userInfo) {
  history.push('/login')
}


// 动态接入子应用
// 从接口中获取子应用配置，export 出的 qiankun 变量是一个 promise
export const qiankun = request.get('/apps').then((res) => {
  const routes = res.map(item => { return { path: item.to, microApp: item.name } })
  const apps = res
  return {
    // 注册子应用信息
    apps,
    routes
  }
});

// 向子应用传递参数
export const useQiankunStateForSlave = () => {
  const [globalState, setQiankunGlobalState] = useState({
    slogan: 'Hello MicroFrontend',
  });
  const [showApp2, setQiankunShowApp2] = useState(false)

  return {
    globalState,
    showApp2,
    setQiankunShowApp2,
    setQiankunGlobalState,
  };
};